package com.sc.nft.task;

import cn.hutool.core.collection.CollUtil;
import com.sc.nft.entity.*;
import com.sc.nft.entity.vo.ExcellentBrandRankingVO;
import com.sc.nft.enums.EcologyPoolLinkTypeEnum;
import com.sc.nft.service.*;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 国库任务
 */
@Component
@RequiredArgsConstructor
@Slf4j
public class EcologyPoolTask {
    private final EcologyPoolDaoRankConfService ecologyPoolDaoRankConfService;
    private final EcologyPoolDaoRankService ecologyPoolDaoRankService;
    private final EcologyPoolSourceDetailService ecologyPoolSourceDetailService;
    private final EcologyPoolRecordService ecologyPoolRecordService;
    private final EcologyPoolCommunityServiceRankConfService ecologyPoolCommunityServiceRankConfService;
    private final EcologyPoolCommunityServiceRankService ecologyPoolCommunityServiceRankService;
    private final EcologyPoolSourceService ecologyPoolSourceService;
    private final EcologyPoolRedPackService ecologyPoolRedPackService;


    /**
     * 红包数量巡检
     */
    @XxlJob("redPackOnSiteInspection")
    public void redPackOnSiteInspection() {
        ecologyPoolRedPackService.redPackOnSiteInspection();
    }


    /**
     * 红包活动结束归还积分
     */
    @XxlJob("redPackExpiredPointsRefund")
    public void redPackExpiredPointsRefund() {
        ecologyPoolRedPackService.redPackExpiredPointsRefund();
    }

    /**
     * 优秀品牌排行榜
     */
    @XxlJob("excellentBrandRanking")
    public void excellentBrandRanking() {
        // 获取当前符合的排行榜活动,把排行榜活动拿出来以后 循环处理
        List<EcologyPoolDaoRankConf> activeRankings = ecologyPoolDaoRankConfService.getActiveRankings();
        if (CollUtil.isEmpty(activeRankings)) {
            return;
        }
        if (CollUtil.isNotEmpty(activeRankings)) {
            activeRankings.forEach(activeRanking -> {
                // 对所有激活排行榜的dao进行排行结算
                List<ExcellentBrandRankingVO> excellentBrandRankingVOS = ecologyPoolDaoRankConfService.excellentBrandRanking(activeRanking);
                if (CollUtil.isEmpty(excellentBrandRankingVOS)) {
                    return;
                }
                // 做入库处理
                ecologyPoolDaoRankService.saveBatchExchequer(excellentBrandRankingVOS);
                //做排名处理
                ecologyPoolDaoRankService.rankProcessing(activeRanking.getId());
            });
        }
    }

    /**
     * 城市服务中心和社区网点排行榜
     */
    @XxlJob("networkServiceCenterRanking")
    public void networkServiceCenterRanking() {
        // 根据任务进行排行榜的处理
        List<EcologyPoolCommunityServiceRankConf> activeRankings = ecologyPoolCommunityServiceRankConfService.getActiveRankings();
        if (CollUtil.isEmpty(activeRankings)) {
            return;
        }
        activeRankings.forEach(ecologyPoolCommunityServiceRankConf -> {
            List<EcologyPoolCommunityServiceRank> list = ecologyPoolCommunityServiceRankConfService.excellentBrandRanking(ecologyPoolCommunityServiceRankConf);
            // 入库
            ecologyPoolCommunityServiceRankService.saveBatchExchequer(list);
            // 排名处理
            ecologyPoolCommunityServiceRankService.rankProcessing(ecologyPoolCommunityServiceRankConf);

        });
    }


    /**
     * 释放的积分总池.
     */
    @XxlJob("pointsDistribution")
    public void pointsDistribution() {
        // 获取当前需要释放的积分列表,获取出来让后循环处理
        List<EcologyPoolSourceDetail> leDateList = ecologyPoolSourceDetailService.getLeDateList();
        if (CollUtil.isEmpty(leDateList)) {
            return;
        }
        // 处理汇总的数据
        List<EcologyPoolSourceDay> ecologyPoolSourceDays = new ArrayList<>();
        // 把leDateList根据LinkDate进行分组
        Map<Date, List<EcologyPoolSourceDetail>> collect = leDateList.stream().collect(Collectors.groupingBy(EcologyPoolSourceDetail::getLinkDate));
        collect.forEach((k, v) -> {
            EcologyPoolSourceDay ecologyPoolSourceDay = new EcologyPoolSourceDay();
            ecologyPoolSourceDay.setLinkType(EcologyPoolLinkTypeEnum.POINTS);
            ecologyPoolSourceDay.setLinkDate(k);
            ecologyPoolSourceDay.setLinkCount(v.stream().map(EcologyPoolSourceDetail::getLinkCount).reduce(BigDecimal::add).orElse(BigDecimal.ZERO));
            ecologyPoolSourceDays.add(ecologyPoolSourceDay);
        });
        // 把ecologyPoolSourceDays数据落库,让后开始发积分,积分一笔一笔处理.
        collect.forEach((date, ecologyPoolSourceDetails) -> {
            // 发放积分.
            ecologyPoolSourceDetails.forEach(ecologyPoolSourceDetail -> {
                ecologyPoolSourceDetailService.pointsDistribution(date, ecologyPoolSourceDetail);
            });
        });
        ecologyPoolRecordService.pointsBatch(ecologyPoolSourceDays);
    }


    /**
     * 把今天需要开始释放的积分总条目状态进行修改,对他进行开始释放
     */
    @XxlJob("startReleasingPoints")
    public void startReleasingPoints() {
        ecologyPoolSourceService.startReleasingPoints();
    }


    /**
     * 把积分释放完毕的条目状态进行修改,对他进行释放完毕
     */
    @XxlJob("finishReleasingPoints")
    public void finishReleasingPoints() {
        ecologyPoolSourceService.finishReleasingPoints();
    }

    /**
     * 城市服务中心和社区网点排行榜自动结算
     */
    @XxlJob("networkServiceCenterRankingAutoSettle")
    public void networkServiceCenterRankingAutoSettle() {
        // 根据任务进行排行榜的处理
        List<EcologyPoolCommunityServiceRankConf> rankings = ecologyPoolCommunityServiceRankConfService.getWaitAutoSettleRankings();
        if (CollUtil.isEmpty(rankings)) {
            return;
        }
        //自动结算
        rankings.forEach(ecologyPoolCommunityServiceRankConfService::autoSettle);
    }

}
